package com.eascs.admin.o2o.o2ozx.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.internal.util.BaseUserUtil;
import com.eascs.admin.o2o.internal.util.BaseUtils;
import com.eascs.admin.o2o.loan.entity.LoanApplyFileEntity;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.o2o.o2ozx.serviceimpl.O2oLoadUserZxServiceImpl;
import com.eascs.admin.o2o.o2ozx.serviceimpl.O2oZxServiceImpl;
import com.eascs.admin.o2o.zx.entity.O2oLoanUserEntityZx;
import com.eascs.admin.o2o.zx.entity.O2oZxEntity;
import com.eascs.admin.o2o.zx.vo.O2oZxVo;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.common.custnew.entity.CustomerEntity;
import com.eascs.crm.common.custnew.service.CustomerService;
import com.eascs.datalist.entity.O2oDataListEntity;
import com.eascs.datalist.service.O2oDataListService;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxConfirmButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.tag.MenuTag;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.service.sys.SysOrgService;

@Controller
@RequestMapping("/o2ozx")
public class O2oZxController extends BaseMasterViewController {

	
	private final Logger logger = LoggerFactory.getLogger(O2oZxController.class);
	

	
	@Reference
	CustomerService customerService;
	
	@Reference
	SysOrgService sysOrgService;
	
	@Autowired
	BaseUserUtil userUtils;
	
	@Autowired
	O2oZxServiceImpl o2oZxServiceImpl;
	
	@Autowired
	O2oLoadUserZxServiceImpl o2oLoadUserZxServiceImpl;
	
	@Autowired
	private O2oDataListService o2oDataListService;
	@Autowired
	private O2oLoanapplyService o2oloanapplyService;
	@Override
	public void preHandle() {
		super.preHandle();
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "loanManage1");
	}

	
	/**
	 * @Title: index
	 * @Description: 新增征信-查询客户
	 * @author yy
	 * @return
	 * @return String
	 * @throws @date
	 *             2017年9月4日 下午20:17:31
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index(String idCard) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "loanManageAddNewZx");
		if(StringUtil.isNotBlank(idCard)){
		   idCard=idCard.trim();
		}
		ViewContext.put("idCard", idCard);
		logger.info("idCard:", idCard);
		return "o2ozx/index";

	}
	
	
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String search(O2oZxVo o2oZxVo) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "nzx_liebiao");
		o2oZxVo.setPageSize(BaseUtils.PAGE_SIZE);
		
		List<User> users=sysOrgService.checkDataPermission(UserHolder.getUser().getNo());
		String no="";
		if (users!=null) {
			for (User user : users) {
				no+=user.getNo()+",";
			}
		}
		if (!"".equals(no)) {
			if (",".equals(no.substring(no.length()-1, no.length()))) {
				no=no.substring(0, no.length()-1);
			}
		}
		logger.info("userno={}", no);
		o2oZxVo.setType(no);
		
		PageResult<Object[]> pageResult = o2oZxServiceImpl.findByPage(o2oZxVo);
		String[] titles = {"单号", "客户姓名", "客户类型", "征信银行", "信贷经理","经办平台", "创建时间", "单据状态","是否作废"};
		SelectDataOption[] selectDataOptions = {null, null,  new SelectDataOption("ZX_TYPE"), new SelectDataOption("BANKTYPEO2O"),null,null,null,new SelectDataOption("O2O_BILLSTATUS"),null};
		StringBuffer pageUrl=new StringBuffer("/o2ozx/list");
		if(o2oZxVo!=null){
			String billCode=o2oZxVo.getBillCode()==null?"":o2oZxVo.getBillCode(); //
			String operatorName=o2oZxVo.getOperatorName()==null?"":o2oZxVo.getOperatorName();
			String zxBank=o2oZxVo.getZxBank()==null?"":o2oZxVo.getZxBank();
			String zxStatus=o2oZxVo.getZxStatus()==null?"":o2oZxVo.getZxStatus();
			String zxType=o2oZxVo.getZxType()==null?"":o2oZxVo.getZxType();
			String userFullName=o2oZxVo.getUserFullName()==null?"":o2oZxVo.getUserFullName();
			String createEndTime=o2oZxVo.getCreateEndTime()==null?"":o2oZxVo.getCreateEndTime();
			String createStartTime=o2oZxVo.getCreateStartTime()==null?"":o2oZxVo.getCreateStartTime();
			String zxEndTime=o2oZxVo.getZxEndTime()==null?"":o2oZxVo.getZxEndTime();
			String zxStartTime=o2oZxVo.getZxStartTime()==null?"":o2oZxVo.getZxStartTime();
			
			pageUrl.append("?billCode="+billCode);
			pageUrl.append("&operatorName="+operatorName);
			pageUrl.append("&zxBank="+zxBank);
			pageUrl.append("&zxStatus="+zxStatus);
			pageUrl.append("&zxType="+zxType);
			pageUrl.append("&userFullName="+userFullName);
			pageUrl.append("&createEndTime="+createEndTime);
			pageUrl.append("&createStartTime="+createStartTime);
			pageUrl.append("&zxEndTime="+zxEndTime);
			pageUrl.append("&zxStartTime="+zxStartTime);
			}
		pageUrl.append("&pageIndex=");
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, pageUrl.toString());
		paginationInfo.setSelectDataOptions(selectDataOptions);
		Button[] titleButton = new Button[] { ButtonHelper.createRedirectButton(null, "/o2ozx/detail?id=") };
		paginationInfo.setTitleButtons(titleButton);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("o2oZxVo", o2oZxVo);
		return "/o2ozx/list";
	}
	
	/**
	 * @Title: search
	 * @Description: 根据查询结果跳转到申请借款-编辑客户信息 或者申请借款-增加客户信息
	 * @author yy
	 * @param idCard
	 * @return
	 * @return String
	 * @throws @date
	 *             2017年9月4日 下午20:17:44
	 */
	@RequestMapping(value = "/search")
	public String search(String idCard) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "nzx_search");
		if(StringUtil.isNotBlank(idCard)){
			   idCard=idCard.trim();
		}
		ViewContext.put("idCard", idCard);
		CustomerEntity basic = customerService.queryCustomerEntityByIdCard(idCard);
		String title = "";
		if (basic == null) {
			basic=new CustomerEntity();
			title = "添加";
		} else {
			title = "编辑";
		}
		ViewContext.put("idCard", idCard);
		ViewContext.put("title", title);
		ViewContext.put("vo", basic);
		return "/o2ozx/editZx";
	}
	
	/**
	 * 新增征信
	 * @param id
	 * @param billCode
	 * @param loanType
	 * @return
	 */
	@RequestMapping(value="/addZx")
	public String toAddloanapply(String customerId,String loanType){
		LoanApplyFileEntity o2oLoanapplyEntity = new LoanApplyFileEntity();
		o2oLoanapplyEntity.setLoanType(loanType);
		ViewContext.put("loanApplyFileEntity", o2oLoanapplyEntity);
		ViewContext.put("customerId", customerId);
		return "o2ozx/addO2oZx";
	}
	
		/**
		 * 申请征信
		 * @Title: zxbgView
		 * @Description: 夫妻双方征信报告模块
		 * @author 张庆奇
		 * @param customerId
		 * @return
		 * @return BaseRestResult<String>
		 * @throws
		 * @date 2017年9月5日 下午6:22:55
		 */
		@RequestMapping("/zxbgView")
		public String zxbgView(String loanapplyNo) {
			
				PageResult<Object[]> paymentsResult = o2oZxServiceImpl.queryFqZxbg(loanapplyNo);
				String[] paymentstitles = { "征信单号","姓名", "征信银行", "银行征信报告时间", "第三方征信报告时间", "更新时间", "更新人","征信状态" };
				PaginationInfo paginationInfo = new PaginationInfo(paymentsResult, paymentstitles);
				SelectDataOption[] selectDataOptions = {null,null, new SelectDataOption("BANKTYPEO2O"),null,null,null,null,new SelectDataOption("O2O_BILLSTATUS")};
				paginationInfo.setSelectDataOptions(selectDataOptions);
				paginationInfo.setBuildPagingNavigation(false);
				paginationInfo.setFirstRenderColumnIndex(2);
				paginationInfo.setBuildRowNumber(false);
			    Button selectButton=ButtonHelper.createAjaxButton("选择征信报告", "/ro2ozx/loanFqZxbg?loanapplyNo="+loanapplyNo+"&IdNum={1}") ;
			    Button createButton=ButtonHelper.createAjaxButton("申请征信", "/ro2ozx/applyZx?loanapplyNo="+loanapplyNo+"&IdNum={1}") ;
				Button[] moreButtons = new Button[] {selectButton,createButton};
				paginationInfo.setMoreButtons(moreButtons);
				Button[] titleButton = new Button[] { ButtonHelper.createRedirectButton(null, "/o2ozx/detail?id=") };
				paginationInfo.setTitleButtons(titleButton);
				ViewContext.put("paginationInfo", paginationInfo);
				return "/o2ozx/zxbgView";
			}
		
		@RequestMapping("/zxbgViewNot")
		public String zxbgViewNotOperation(String loanapplyNo) {
			
				PageResult<Object[]> paymentsResult = o2oZxServiceImpl.queryFqZxbg(loanapplyNo);
				String[] paymentstitles = { "征信单号","姓名", "征信银行", "银行征信报告时间", "第三方征信报告时间", "更新时间", "更新人","征信状态" };
				PaginationInfo paginationInfo = new PaginationInfo(paymentsResult, paymentstitles);
				SelectDataOption[] selectDataOptions = {null,null, new SelectDataOption("BANKTYPEO2O"),null,null,null,null,new SelectDataOption("O2O_BILLSTATUS")};
				paginationInfo.setSelectDataOptions(selectDataOptions);
				paginationInfo.setBuildPagingNavigation(false);
				paginationInfo.setFirstRenderColumnIndex(2);
				paginationInfo.setBuildRowNumber(false);
				Button[] titleButton = new Button[] { ButtonHelper.createRedirectButton(null, "/o2ozx/detail?id=") };
				paginationInfo.setTitleButtons(titleButton);
				ViewContext.put("paginationInfo", paginationInfo);
				return "/o2ozx/zxbgView";
			}
	

	/**
	 * 修改详情页面
	 * 
	 * @param loanapplyNo
	 * @param massege
	 * @return
	 */
	@RequestMapping(value = "/findO2oZx")
	public String uploadfile(String id, String massege) {
		
		O2oZxEntity o2oZxEntity  = o2oZxServiceImpl.getEntityById(id);
		
		O2oLoanUserEntityZx o2oLoanUserEntityZx = o2oLoadUserZxServiceImpl.getEntityByZxNo(id);
		
		/**** 查询附件 ****/
		List<O2oDataListEntity> o2oDataList = o2oDataListService.findByEntityList(o2oZxEntity.getId());
		
		ViewContext.put("o2oDataList", o2oDataList);
		ViewContext.put("errorMassege", massege);
		ViewContext.put("o2oZxEntity", o2oZxEntity);
		ViewContext.put("o2oLoanUserEntityZx", o2oLoanUserEntityZx);
		
		return "o2ozx/uploadfile";
	}
	
	/**
	 * 修改详情页面
	 * 
	 * @param loanapplyNo
	 * @param massege
	 * @return
	 */
	@RequestMapping(value = "/detail")
	public String detail(String id, String massege) {
//		User user = UserHolder.getUser();
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "detail_nzx_liebiao");
		O2oZxEntity o2oZxEntity  = o2oZxServiceImpl.getEntityById(id);
		
		O2oLoanUserEntityZx o2oLoanUserEntityZx = o2oLoadUserZxServiceImpl.getEntityByZxNo(id);
		
		Button button = ButtonHelper.createAjaxButton("启动流程", "/work/newstartup?loanapplyNo=" + o2oZxEntity.getId());
		button.setPermissionCode("NewZxStartFlowButton");
		//*****loan-12273 征信查询单据删除功能 start
		AjaxConfirmButton delBtn = new AjaxConfirmButton("删除",  "/ro2ozx/delete?id=" + o2oZxEntity.getId());
		delBtn.setPermissionCode("loanManageDeleteNewZx");
		delBtn.setConfirmMessage("删除数据不可恢复，确认删除？");
		List<Button> btnS = new ArrayList<Button>();
		btnS.add(button);
		btnS.add(delBtn);
		//**********end
		ViewContext.put("buttons", btnS);
		String approvUrl = "/wf/processNode/agreeInfo?referId="+o2oZxEntity.getId()+"&amp;processType=CREDIT_INQUIRY";
		String approvUrl1="";
		if(o2oZxEntity.getLoanapplyNo()!=null){
			O2oLoanapplyEntity o2oLoanapplyEntity =o2oloanapplyService.findById(o2oZxEntity.getLoanapplyNo());
			if (o2oLoanapplyEntity!=null) {
				if (o2oLoanapplyEntity.getLoanType().equals("BP")||o2oLoanapplyEntity.getLoanType().equals("BP_JX")||o2oLoanapplyEntity.getLoanType().equals("SG")||o2oLoanapplyEntity.getLoanType().equals("YC")||o2oLoanapplyEntity.getLoanType().equals("SC")) {
					approvUrl1 = "/wf/processNode/agreeInfo?referId="+o2oZxEntity.getLoanapplyNo()+"&amp;processType=CREDIT_BP";
				}else if (o2oLoanapplyEntity.getLoanType().equals("FQ")||o2oLoanapplyEntity.getLoanType().equals("EA")) {
					approvUrl1 = "/wf/processNode/agreeInfo?referId="+o2oZxEntity.getLoanapplyNo()+"&amp;processType=CREDIT_FQ";
				}else {
					approvUrl1 = "/wf/processNode/agreeInfo?referId="+o2oZxEntity.getLoanapplyNo()+"&amp;processType=CREDIT";
					
				}
			}
//			approvUrl = "/wf/processNode/agreeInfo?referId="+o2oZxEntity.getLoanapplyNo()+"&amp;processType=PROCESS_ACCESS";
		}
		/**** 查询附件 ***
		List<O2oDataListEntity> o2oDataList = o2oDataListService.findByEntityList(o2oZxEntity.getId());
		ViewContext.put("o2oDataList", o2oDataList);*/
		ViewContext.put("errorMassege", massege);
		ViewContext.put("approvUrl", approvUrl);
		ViewContext.put("approvUrl1", approvUrl1);
		ViewContext.put("o2oZxEntity", o2oZxEntity);
		ViewContext.put("o2oLoanUserEntityZx", o2oLoanUserEntityZx);
		String datalistReadonly = "true";
		/*if(StatusContexts.WFTYPE_STATUS_CLO.equals(o2oZxEntity.getZxStatus())){
			datalistReadonly = "true";
		}
		if(user.getNo().equals(o2oZxEntity.getOperatorId())||user.getNo().equals(o2oZxEntity.getCreateId())||
				user.isSuperAdministrator()
				){
			datalistReadonly = "false";
		}*/
		ViewContext.put("datalistReadonly", datalistReadonly);
		return "o2ozx/detail";
	}
	
	/**
	 * @Title:
	 * @Description: 补充征信附件信息
	 * @author CXW
	 * @return
	 * @return String
	 * @throws @date 2018年3月8日 下午2:17:31
	 */
	@RequestMapping("/supplementFile")
	public String supplementFile(String loanapplyNo, String readonly) {
		if (StringUtil.isBlank(readonly)) {
			readonly = "false";
		}
		o2oZxServiceImpl.supplementFile(loanapplyNo, readonly);
		return "o2ozx/detailUploadView";
	}
}
